Imports System.IO
Imports System.Data.SqlClient
Imports System
Imports System.Text
Imports System.Data.Odbc
Imports System.Security.Cryptography
Public Class FrmAddUser
    Public GV_ISEDIT As Boolean = False
    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        GV_ISEDIT = False
        Panel2.Enabled = True
        btnEdit.Visible = False
        btnDel.Visible = False
        BtnAdd.Visible = False
        btnSave.Visible = True
        btnCancel.Visible = True
        ClearText()
    End Sub
    Private Sub ClearText()
        txtLoginID.Text = ""
        txtlastName.Text = ""
        TxtFirstName.Text = ""
        TxtEmail.Text = ""
        TxtPw.Text = ""
        TxtConfirmPW.Text = ""
    End Sub

    Private Sub FrmAddUser_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        BtnAdd_Click(sender, e)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        If GV_ISEDIT = False Then ClearText()
        Panel2.Enabled = False
        BtnAdd.Visible = True
        btnEdit.Visible = True
        btnDel.Visible = True
        btnSave.Visible = False
        btnCancel.Visible = False
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Trim(txtLoginID.Text) = "" Then
            MsgBox("Login Id cannot be blank.", MsgBoxStyle.Information)
            Exit Sub
        End If

        If Trim(txtlastName.Text) = "" Then
            MsgBox("Last name cannot be blank.", MsgBoxStyle.Information)
            Exit Sub
        End If

        If Trim(TxtFirstName.Text) = "" Then
            MsgBox("First name cannot be blank.", MsgBoxStyle.Information)
            Exit Sub
        End If

        If Trim(TxtPw.Text) = "" Then
            MsgBox("Password cannot be blank.", MsgBoxStyle.Information)
            Exit Sub
        End If

        If Trim(TxtPw.Text) <> Trim(TxtConfirmPW.Text) Then
            MsgBox("Confrim password must be same with password.", MsgBoxStyle.Information)
            Exit Sub
        End If

        SaveNewUser()
        Close()
    End Sub
    Private Sub SaveNewUser()
        Dim myCommand As SqlCommand
        Dim SqlConn As New SqlConnection()
        SqlConn = New SqlConnection(GV_CONNETION)
        myCommand = New SqlCommand("spInsertNewUser", SqlConn)
        myCommand.CommandType = CommandType.StoredProcedure
        SqlConn.Open()

        myCommand.Parameters.Add("@UserLoginId", SqlDbType.VarChar)
        myCommand.Parameters.Add("@UserPassword", SqlDbType.Binary)
        myCommand.Parameters.Add("@UserFirstName", SqlDbType.VarChar)
        myCommand.Parameters.Add("@UserLastName", SqlDbType.VarChar)
        myCommand.Parameters.Add("@UserEmail", SqlDbType.VarChar)
        myCommand.Parameters.Add("@EnteredBy", SqlDbType.VarChar)

        myCommand.Parameters("@UserLoginId").Value = Trim(txtLoginID.Text)
        myCommand.Parameters("@UserPassword").Value = GenerateMD5_Pw(Trim(TxtPw.Text))
        myCommand.Parameters("@UserFirstName").Value = Trim(TxtFirstName.Text)
        myCommand.Parameters("@UserLastName").Value = Trim(txtlastName.Text)
        myCommand.Parameters("@UserEmail").Value = Trim(TxtEmail.Text)
        myCommand.Parameters("@EnteredBy").Value = GV_USER
        myCommand.ExecuteNonQuery()

        myCommand.Cancel()
        SqlConn.Close()
        myCommand = Nothing
        SqlConn = Nothing
    End Sub
    Function GenerateMD5_Pw(ByVal vb4EncryptPW) As Byte()
        Dim md5Hasher As New MD5CryptoServiceProvider()

        Dim hashedBytes As Byte()
        Dim encoder As New UTF8Encoding()

        hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(vb4EncryptPW))
        GenerateMD5_Pw = hashedBytes


    End Function
End Class